//
//  JHJTabBarCollectionViewCell.m
//  项目
//
//  Created by dllo on 16/5/8.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "JHJTabBarCollectionViewCell.h"
#import "STKAudioPlayerHandle.h"
#import <UIImageView+WebCache.h>

@interface JHJTabBarCollectionViewCell ()
@property (nonatomic, retain)CABasicAnimation *ajkkd;

@property (nonatomic, retain)CABasicAnimation *rotationAnimation;


@end

@implementation JHJTabBarCollectionViewCell


- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatView];
    }
    return self;
}

- (void)creatView {
    self.myImageView = [[UIImageView alloc] init];
    [self.contentView addSubview:self.myImageView];
    self.myLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 10, WIDTH - 90, 20)];
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(80, 40, WIDTH - 90, 20)];
    [self.contentView addSubview:self.myLabel];
    [self.contentView addSubview:self.nameLabel];
    self.myLabel.textAlignment = 1;
    self.nameLabel.textAlignment = 1;
    self.myImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.myImageView addGestureRecognizer:tap];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationAction:) name:@"Animation" object:nil];
    
}
-(void)notificationAction:(NSNotification *)notification
{
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.duration = 2;
    rotationAnimation.fromValue = @(0);
    rotationAnimation.toValue = @(M_PI * 2);
    rotationAnimation.repeatCount = NSIntegerMax;
    rotationAnimation.cumulative = YES;
    self.ajkkd = rotationAnimation;
    self.rotationAnimation = rotationAnimation;
    [self.myImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];

}
- (void)setSongsModel:(PosterDetailSongsModel *)songsModel {
    _songsModel = songsModel;
    [self.myImageView sd_setImageWithURL:[NSURL URLWithString:[_songsModel picUrl]]];
    self.myLabel.text = songsModel.name;
    self.nameLabel.text = songsModel.singerName;
//    if ([[[STKAudioPlayerHandle shareSTKAudioPlayerHandle] audioPlayer] state] == STKAudioPlayerStatePlaying) {
//        NSLog(@"111");
//        CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//        rotationAnimation.duration = 2;
//        rotationAnimation.fromValue = @(0);
//        rotationAnimation.toValue = @(M_PI * 2);
//        rotationAnimation.repeatCount = NSIntegerMax;
//        rotationAnimation.cumulative = YES;
//        self.rotationAnimation = rotationAnimation;
//        [self.myImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
//        [self rotationOrStop];
//        NSLog(@"%@",self.rotationAnimation);
//    }
    CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
    rotationAnimation.duration = 2;
    rotationAnimation.fromValue = @(0);
    rotationAnimation.toValue = @(M_PI * 2);
    rotationAnimation.repeatCount = NSIntegerMax;
    rotationAnimation.cumulative = YES;
    self.rotationAnimation = rotationAnimation;
    [self.myImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
    [self rotation];
//    NSLog(@"%@",self.rotationAnimation);
//    NSLog(@"%@",self.ajkkd);
}



- (void)tapAction:(UITapGestureRecognizer *)tap {
//    if (!self.rotationAnimation) {
//        CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
//        rotationAnimation.duration = 2;
//        rotationAnimation.fromValue = @(0);
//        rotationAnimation.toValue = @(M_PI * 2);
//        rotationAnimation.repeatCount = NSIntegerMax;
//        rotationAnimation.cumulative = YES;
//        self.rotationAnimation = rotationAnimation;
//        [self.myImageView.layer addAnimation:rotationAnimation forKey:@"rotationAnimation"];
//        NSLog(@"3333");
//    }
    NSLog(@"1111");
    if ([[[STKAudioPlayerHandle shareSTKAudioPlayerHandle] audioPlayer] state] == STKAudioPlayerStatePlaying) {
        self.isPlay = YES;
    } else {
        self.isPlay = NO;
    }
    self.cellBlock(self.isPlay);
    [self rotationOrStop];
}
- (void)rotation {
//    NSLog(@"11111");
    CFTimeInterval stopTime = self.myImageView.layer.timeOffset;
    self.myImageView.layer.timeOffset = 0;
    // 设置速度为1.0;
    self.myImageView.layer.speed = 1.0;
    //重置开始时间.
    self.myImageView.layer.beginTime = 0;
    //        // 设置开始时间.
    // 开始时间 = 当前时间 - 偏移时间.
    self.myImageView.layer.beginTime = [self.myImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil] - stopTime;
}

- (void)rotationOrStop {
    NSLog(@"ksafksddf");
    if (!self.isPlay) {
        NSLog(@"11");
        [self rotation];
        
    } else {
        NSLog(@"22");
        [self stopRotation];
    }
    self.isPlay = !self.isPlay;
}
- (void)stopRotation {
    // 每一个view 的layer层系统都记录了时间的属性, 通过改变view动画时间来控制动画效果.
    // 获得到当前旋转的时间点.
    CFTimeInterval stopTime = [self.myImageView.layer convertTime:CACurrentMediaTime() fromLayer:nil];
    // 设置动画的播放速度为0, 动画停止.
    self.myImageView.layer.speed = 0;
    // 记录当前的时间偏移量.
    self.myImageView.layer.timeOffset = stopTime;
    
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.myImageView.frame = CGRectMake(5, 5, 70, 70);
    self.myImageView.layer.cornerRadius = 35;
    self.myImageView.layer.masksToBounds = YES;
    
}

@end
